//
//  RootController.m
//  Ghac 4S
//
//  Created by apple on 11-10-14.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.

#import "RootController.h"
#import "MainEntry.h"
#import "LocationController.h"
#import "ContactController.h"
#import "NewsControler.h"
#import "MyStatusBarController.h"
#import "MyNavigationBarController.h"
#import "MyWeatherBottomBarController.h"
#import "InnerViewController.h"
#import "InnerView.h"
#import <QuartzCore/QuartzCore.h>
@implementation RootController
@synthesize pageContainer;
@synthesize pageControlBar;
@synthesize scrollPagesView;
@synthesize detailPagesView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
        m_pageViewControllers = [[NSMutableArray alloc] init];
       m_navigationBarController = [[MyNavigationBarController alloc] initWithNibName:@"MyNavigationBarController" bundle:[NSBundle mainBundle]];
    }
    return self;
}
- (void)dealloc
{
    [m_pageViewControllers release];
    [m_navigationBarController release];
    [m_innerViewController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initforMainFrame];
    [self initForScrollPages];
    [self initFotInnerView];
    
}
- (void)initForScrollPages
{
    
    // set up bottom bar
    MyWeatherBottomBarController* barController = [[MyWeatherBottomBarController alloc] init];
    CGRect barFrame = barController.view.frame;
    [barController.view setFrame:CGRectMake(0, pageContainer.superview.frame.size.height - barFrame.size.height, barFrame.size.width, barFrame.size.height)];
    [self.scrollPagesView addSubview:barController.view];
    
    [m_pageViewControllers addObject:[[[NewsControler alloc] init] autorelease]];
    [m_pageViewControllers addObject:[[[MainEntry alloc] init] autorelease]];
    [m_pageViewControllers addObject:[[[ContactController alloc] init] autorelease]];
    
    [self.scrollPagesView bringSubviewToFront:pageContainer];
    [self.scrollPagesView bringSubviewToFront:pageControlBar];
    pageContainer.delegate = self;
    pageContainer.pageCount = m_pageViewControllers.count;
    pageContainer.currentPage = 1;
    [pageControlBar addTarget:self action:@selector(controlChangePage:) forControlEvents:UIControlEventValueChanged];
    [pageControlBar setNumberOfPages:pageContainer.pageCount];
    
    
}
-(void) initforMainFrame
{
    MyStatusBarController* stateBarControl = [[MyStatusBarController alloc] init];
    [self.view addSubview:stateBarControl.view];
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    //set up navigation frame
    [m_navigationBarController.view setFrame:CGRectMake(0, 30, frame.size.width, m_navigationBarController.view.frame.size.height)];
    [self.view addSubview:m_navigationBarController.view];
}
-(void) initFotInnerView
{
    m_innerViewController = [[InnerViewController alloc] init];
    [self.detailPagesView addSubview:m_innerViewController.view];
    [self.detailPagesView setHidden:YES];
    
}
-(void)showInnerView:(UIViewController<InnerViewAction>*)contentViewController
{
    [self.detailPagesView setHidden:NO];
    m_innerViewController.innerViewController = contentViewController;
    [self.detailPagesView.superview bringSubviewToFront:self.detailPagesView];
    CGRect frame = self.detailPagesView.frame;
    frame.origin.y = frame.size.height;
    [self.detailPagesView setFrame:frame];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.detailPagesView.superview cache:YES];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.5];
    frame.origin.y = 0;
    [self.detailPagesView setFrame:frame];
    [UIView commitAnimations];
}
-(void)backToEntryView
{
    CGRect frame = self.detailPagesView.frame;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"innerHide" context:context];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.detailPagesView.superview cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.5];
    frame.origin.y = frame.size.height;
    [self.detailPagesView setFrame:frame];
    [UIView commitAnimations];
//    [self.detailPagesView setHidden:YES];
}
/**
 *   delegate;
 *
 *
 */
- (void)controlChangePage:(id)sender
{
    pageContainer.currentPage = pageControlBar.currentPage;
}
-(void)scrollPagesContainerDidChangePage:(ScrollPagesContainer *)container page:(NSInteger)page
{
    pageControlBar.currentPage = page;
}
-(UIView *)scrollPagesContainerLoadPage:(ScrollPagesContainer *)container forIndex:(NSInteger)index
{
    return [[m_pageViewControllers objectAtIndex:index] view];
}
-(void)scrollPagesContainerDidScroll:(ScrollPagesContainer *)container
{
    
}
-(void)scrollPagesContainerDidFinishedAnimation:(ScrollPagesContainer *)container
{
    
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
